/**
* Created by wl
* 基金储备库-编辑管理机构信息
*/
<template>
    <div :class="{'ca-box': $route.name.includes('add'), 'ca-m-t-28': $route.name.includes('add'), 'ca-p-20': $route.name.includes('add')}">
        <div class="ca-title">
            <h3>管理机构信息</h3>
            <div class="ca-align-right" v-if="$route.params.itemId != '-1' && !$route.name.includes('add')">
                <el-button type="primary" size="small" @click="save()">保存</el-button>
                <el-button size="small" @click="cancel()">取消</el-button>
            </div>
        </div>
        <el-form ref="form1" label-width="150px" :model="fundMechanism" :rules="rules">
            <el-row :gutter="10">
                <el-col :span='11'>
                    <el-form-item label="机构名称：" label-width="160px">
                        <el-input style="width:100%" v-model="fundMechanism.mechanismName" :maxlength="100"></el-input>
                    </el-form-item>
                    <el-form-item label="执行事务合伙人：" label-width="160px">
                        <el-input style="width:100%" v-model="fundMechanism.partner" :maxlength="100"></el-input>
                    </el-form-item>
                    <el-form-item label="成立时间：" label-width="160px">
                        <el-date-picker
                        type="date"
                        placeholder="选择日期"
                        style="width:100%"
                        v-model="fundMechanism.foundingTime">
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item label="认缴金额(万元)：" label-width="160px" prop="subscribed">
                        <el-input type="number" style="width:100%" v-model="fundMechanism.subscribed"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span='11'>
                    <el-form-item label="法定代表人：" label-width="160px">
                        <el-input style="width:100%" v-model="fundMechanism.representative" :maxlength="100"></el-input>
                    </el-form-item>
                    <!-- <el-form-item label="注册地址：" label-width="160px">
                        <el-input style="width:100%"></el-input>
                    </el-form-item> -->
                    <el-form-item label="注册资本(万元)：" label-width="160px" prop="register">
                        <el-input type="number" style="width:100%" v-model="fundMechanism.register"></el-input>
                    </el-form-item>
                    <el-form-item label="实缴金额(万元)：" label-width="160px" prop="receipts">
                        <el-input type="number" style="width:100%" v-model="fundMechanism.receipts"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="10">
                <el-col :span="22">
                    <csRegion :address='registeredAddress' ref="regist" :title="'注册地址：'" :labelWidth="'160px'"></csRegion>
                    <el-form-item label="股东出资金额和比例：" label-width="160px">
                        <el-input type="text" style="width:100%" v-model="fundMechanism.proportion"></el-input>
                    </el-form-item>
                    <el-form-item label="公司治理：" label-width="160px">
                        <el-input type="textarea" style="width:100%" v-model="fundMechanism.govern" :maxlength="4000"></el-input>
                    </el-form-item>
                    <el-form-item label="内控机制：" label-width="160px">
                        <el-input type="textarea" style="width:100%" v-model="fundMechanism.mechanism" :maxlength="4000"></el-input>
                    </el-form-item>
                    <el-form-item label="管理制度：" label-width="160px">
                        <el-input type="textarea" style="width:100%" v-model="fundMechanism.rule" :maxlength="4000"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
    </div>
</template>
<script type="text/ecmascript-6">
    import {api} from '../../api';
    import {STATUS_HTTP_SUCCESS, apiHttp} from '__service/csHttp';
    export default {
        data() {
            // var validateFourNumber = (rule, value, callback) => {
            //     var reg = /^\d+(\.\d{1,4})?$/;
            //     if (value && (!reg.test(value))) {
            //         callback(new Error('请输入正确范围的数字'));
            //     } else {
            //         callback();
            //     }
            // }
            return {
                registeredAddress: {
                    id: '',
                    businessId: '',
                    province: '',
                    city: '',
                    district: '',
                    businessType: 1,
                    registrationDetailed: ''
                },//注册地址
                fundMechanism: {
                    foundingTime: '', //成立时间
                    fundId: '', //基金id
                    govern: '', //公司治理
                    id: '', //主id
                    mechanism: '', //内控机制
                    mechanismName: '', //机构名称
                    partner: '', //执行事务合伙人
                    proportion: null, //股东出资金额和比例
                    receipts: null, //实收资本
                    region: [ //注册地址
                        {
                            businessId: '',
                            businessType: 1,
                            city: '',
                            district: '',
                            id: '',
                            province: '',
                            registrationDetailed: ''
                        }
                    ],
                    register: null, //注册资本
                    representative: '', //法定代表人
                    rule: '', //管理制度
                    subscribed: null //认缴金额
                },
                rules: {
                    subscribed: [
                        { validator: this.validateMoneyNumber, message: '请输入正确范围金额', trigger: 'blur'}
                    ],
                    register: [
                        { validator: this.validateMoneyNumber, message: '请输入正确范围金额', trigger: 'blur'}
                    ],
                    receipts: [
                        { validator: this.validateMoneyNumber, message: '请输入正确范围金额', trigger: 'blur'}
                    ],
                },
            }
        },
        created() {
        },
        mounted() {
            this.$emit('fundMechanism', this.fundMechanism);
            if (this.$route.params.itemId && this.$route.params.itemId !== '-1') {
                this.getAgencyInfo()
            }
        },
        props: {
        },
        watch: {
            // 深度 watcher
            fundMechanism: {
                handler: function (fundMechanism) {
                    this.$refs.form1.validate((valid) => {
                        if (valid) {
                            this.$emit('fundMechanism', fundMechanism);
                        } else {
                            return false;
                        }
                    })
                },
                deep: true
            },
            registeredAddress: {
                handler: function (registeredAddress) {
                    this.fundMechanism.region = []
                    this.fundMechanism.region.push(registeredAddress)
                    this.$refs.form1.validate((valid) => {
                        if (valid) {
                             this.$emit('fundMechanism', this.fundMechanism);
                        } else {
                            return false;
                        }
                    })
                },
                deep: true
            },
        },
        methods: {
            cancel() {
                this.$emit('close')
            },
            getAgencyInfo() {
                apiHttp(api.CHECK_MECHANISMINFO, {fundId: this.$route.params.itemId}).then(ret => {
                    if (ret.resCode === STATUS_HTTP_SUCCESS) {
                        this.fundMechanism = ret.data;
                        if (this.fundMechanism.region && this.fundMechanism.region.length > 0) {
                            this.registeredAddress = this.fundMechanism.region[0]
                            this.$refs.regist.inview(this.registeredAddress)
                        }
                    }
                })
            },
            save() {
                this.$refs.form1.validate((valid) => {
                    if (valid) {
                        this.fundMechanism.region = []
                        if (this.registeredAddress.province) {
                            this.fundMechanism.region.push(this.registeredAddress)
                        }
                        apiHttp(api.SAVE_MECHANISMINFO, this.fundMechanism).then(ret => {
                            if (ret.resCode === STATUS_HTTP_SUCCESS) {
                                this.$message({
                                    message: '数据保存成功！',
                                    type: 'success',
                                    center: true
                                });
                                this.$emit('update')
                            }
                        })
                    } else {
                        return false;
                    }
                });
            },
            //验证
            validateAddInfo() {
                let isSave = null;
                this.$refs.form1.validate((valid) => {
                    if (valid) {
                        isSave = true;
                    } else {
                        isSave = false;
                    }
                })
                return isSave;
            },
        },
        components: {
             csRegion: require('__components/region'),//地区
        },
    }
</script>